<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Network Configuration</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f0f0f0;
        }
        .container {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 90%;
            max-width: 400px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
        }
        .form-group button {
            width: 100%;
            padding: 10px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .form-group button:hover {
            background-color: #0056b3;
        }
        @media (max-width: 600px) {
            .container {
                padding: 15px;
            }
            .form-group input, .form-group button {
                padding: 12px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>Network Configuration</h2>
        <form id="network-config-form">
            <div class="form-group">
                <label for="ip">Server IP and Port (xxx.xxx.xxx.xxx:port):</label>
                <input type="text" id="ip" name="ip" required>
            </div>
            <div class="form-group">
                <label for="ssid">SSID:</label>
                <input type="text" id="ssid" name="ssid" required>
            </div>
            <div class="form-group">
                <label for="password">Password:</label>
                <input type="password" id="password" name="password" required>
            </div>
            <div class="form-group">
                <button type="submit">Submit</button>
            </div>
        </form>
    </div>
    <script>
        document.getElementById('network-config-form').addEventListener('submit', function(event) {
            event.preventDefault();
            const ipPort = document.getElementById('ip').value;
            const ssid = document.getElementById('ssid').value;
            const password = document.getElementById('password').value;
            console.log('IP and Port:', ipPort);
            console.log('SSID:', ssid);
            console.log('Password:', password);

            const xhr = new XMLHttpRequest();
            xhr.open('POST', `http://${ipPort}/configure-network`, true);
            xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
            xhr.timeout = 5000; // Set timeout to 5 seconds (5000 milliseconds)

            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        alert('Success: ' + xhr.responseText);
                    } else {
                        alert('Error: ' + xhr.statusText);
                    }
                }
            };

            xhr.ontimeout = function () {
                alert('Error: Request timed out');
            };

            xhr.send(JSON.stringify({ ssid: ssid, password: password }));
        });
    </script>
</body>
</html>
